package Poly3;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:面向对象的实现方法
 * User:lenovo
 * Date:2024-06-05
 * Time:15:52
 */
public class SmallChangeSysOop {
    private Scanner scanner=new Scanner(System.in);
    private boolean loop=true;

    //1.零钱通明细
    private String key="";
    //通过字符串details拼接信息，再将它打印出来
    private String details="--------------零钱通明细---------------";

    //2.收益入账
    private double money=0;
    private double balance=0;//余额
    //Date date=new Date();
    private Date date=null;
    //SimpleDateFormat:格式化当前时间
    private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");

    //3.消费
    private String note="";

    //4.退出
    private String choice="";

    public void mainMenu(){
        //主菜单
        do{
            System.out.println("\n=============零钱通项目（OOP）===========");
            System.out.println("\t\t\t1.零钱通明细");
            System.out.println("\t\t\t2.收益入账 ");
            System.out.println("\t\t\t3.消费    ");
            System.out.println("\t\t\t4.退出    ");

            System.out.println("请输入你的选择（1~4）：");
            key=scanner.next();
            //next可以接收一个字符串
            //switch控制语句
            switch (key){
                case "1":
                    this.details();
                    break;//若break不在循环体里面，则要修改为return;
                case "2":
                    this.income();
                    break;
                case "3":
                    this.consume();
                    break;
                case "4":
                   this.exit();
                    break;
                default:
                    System.out.println("选择有误，请重新选择！");
            }
        }while(loop);
        System.out.println("========项目结束！========");
    }

    public void details(){
        //1.零钱通明细
        System.out.println(details);
    }

    public void income(){
        //2.收益入账
        System.out.println("请输入入账金额：");
        money=scanner.nextDouble();
        //money的值需要校验，一会写
        //思路：找出金额不符合的条件，而不是分成两部分，一部分金额正确，一部分金额不正确
        while(true){
            if(money<=0){
                System.out.println("收入金额有误，请重新输入");
                money=scanner.nextDouble();
            }else
                break;
        }
        balance+=money;
        date=new Date();
        details+="\n收益入账：\t+"+money+"\t"+(sdf.format(date))+"\t"+balance;
    }

    public void consume(){
        //3.消费
        System.out.println("请输入消费金额：");
        money=scanner.nextDouble();
        //money的值需要校验，一会写
        while(true){
            if(money<=0||money>balance){
                System.out.println("消费金额不正确，应该在0~"+balance+"请重新输入：");
                money=scanner.nextDouble();
            }else
                break;
        }
        System.out.println("请输入消费说明：");
        note=scanner.next();
        balance-=money;
        date=new Date();
        details+="\n"+note+"\t\t-"+money+"\t"+(sdf.format(date))+"\t"+balance;
    }

    public void exit(){
        //4.退出
        while(true){
            System.out.println("你确定要退出吗？y/n");
            choice=scanner.next();
            if(choice.equals("y")||choice.equals("n")){
                break;
            }
        }
        if(choice.equals("y"))
            loop=false;
    }
}
